package com.pdg.dungeondelver.defaulttheme.renderers;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;

import com.pdg.dungeondelver.client.constants.GameConstants;
import com.pdg.dungeondelver.client.constants.ImageConstants;
import com.pdg.dungeondelver.client.constants.PropertyGroupConstants;
import com.pdg.dungeondelver.client.constants.UiConstants;
import com.pdg.dungeondelver.configuration.Configuration;
import com.pdg.dungeondelver.engine.Atlas;
import com.pdg.dungeondelver.engine.AtlasDirections;
import com.pdg.dungeondelver.engine.Layer;

public class DefaultAtlasRenderer extends BaseRenderer{

	private Integer x=null;
	private Integer y=null;
	private Integer columnWidth=null;
	private Integer rowHeight=null;
	private Integer columns = null;
	private Integer rows = null;
	
	public DefaultAtlasRenderer(Configuration configuration) {
		super(configuration);
	}

	public Integer getX(){
		if(x==null){
			x = getConfiguration().getPropertyGroups().get(PropertyGroupConstants.UI_CONSTANTS).<Integer>get(UiConstants.ATLAS_RENDERER_X);
		}
		return x;
	}
	
	public Integer getY(){
		if(y==null){
			y = getConfiguration().getPropertyGroups().get(PropertyGroupConstants.UI_CONSTANTS).<Integer>get(UiConstants.ATLAS_RENDERER_Y);
		}
		return y;
	}
	
	public Integer getColumnWidth(){
		if(columnWidth==null){
			columnWidth = getConfiguration().getPropertyGroups().get(PropertyGroupConstants.UI_CONSTANTS).<Integer>get(UiConstants.ATLAS_RENDERER_COLUMN_WIDTH);
		}
		return columnWidth;
	}
	
	public Integer getRowHeight(){
		if(rowHeight==null){
			rowHeight = getConfiguration().getPropertyGroups().get(PropertyGroupConstants.UI_CONSTANTS).<Integer>get(UiConstants.ATLAS_RENDERER_ROW_HEIGHT);
		}
		return rowHeight;
	}
	
	public Integer getColumns(){
		if(columns==null){
			columns = getConfiguration().getPropertyGroups().get(PropertyGroupConstants.GAME_CONSTANTS).<Integer>get(GameConstants.ATLAS_COLUMNS);
		}
		return columns;
	}
	
	public Integer getRows(){
		if(rows==null){
			rows = getConfiguration().getPropertyGroups().get(PropertyGroupConstants.GAME_CONSTANTS).<Integer>get(GameConstants.ATLAS_ROWS);
		}
		return rows;
	}
	

	@Override
	public void render(Graphics g,Atlas atlas) {
		for(Integer column=0;column<getColumns();++column){
			for(Integer row=0;row<getRows();++row){
				Color oldColor = g.getColor();
				g.setColor(Color.white);
				Integer x =getX()+column*getColumnWidth();
				Integer y = getY()+row*getRowHeight();
				Layer cell = atlas.get(column).get(row).get(0);
				getConfiguration().getImageManager().getImage(ImageConstants.MINIMAP_PASSAGEWAY_BASE).draw(x, y);
				if(cell.getConnection(AtlasDirections.NORTH)){
					getConfiguration().getImageManager().getImage(ImageConstants.MINIMAP_PASSAGEWAY_NORTH).draw(x, y);
					
				}
				if(cell.getConnection(AtlasDirections.EAST)){
					getConfiguration().getImageManager().getImage(ImageConstants.MINIMAP_PASSAGEWAY_EAST).draw(x, y);
					
				}
				if(cell.getConnection(AtlasDirections.SOUTH)){
					getConfiguration().getImageManager().getImage(ImageConstants.MINIMAP_PASSAGEWAY_SOUTH).draw(x, y);
					
				}
				if(cell.getConnection(AtlasDirections.WEST)){
					getConfiguration().getImageManager().getImage(ImageConstants.MINIMAP_PASSAGEWAY_WEST).draw(x, y);
					
				}
				g.setColor(oldColor);
			}
		}
	}

}
